const render = require("../instances/render");
const Model = require("../models/index").models;
const Record = Model.Record;
const Employee = Model.Employee;

module.exports = (router) => {
    router.get("/", function*(){
        var ctx = this;
        var errorCode = ctx.request.query.errorCode;
        ctx.body = yield render("index.html",{errorCode});
    });

    router.get("/index", function*(){
        var ctx = this;
        ctx.redirect("/");
    });

    router.get("/token/home", function*(){
        var ctx = this;
        var page = ctx.request.query.page || 1;
        page = parseInt(page);
        var records = yield Record.findAll({
            order: [["createdAt","DESC"]],
            limit: 10,
            offset: 10 * (page - 1),
            include : {
                model : Employee,
                attributes:{
                    exclude : ["fingerprint"]
                }
            }
        });
        var count = yield Record.count();
        ctx.body = yield render("home.html", {page,records,count});
    });

    router.get("/token/crew", function*(){
        var ctx = this;
        var id = ctx.request.query.id  || 1;
        var employee = yield Employee.find({
            where : {
                id : id
            },
            attributes:{
                exclude : ["fingerprint"]
            }
        });
        if(!employee){
            ctx.redirect("/token/home");
            return;
        }
        var records = yield Record.findAll({
            where : {
                EmployeeId : employee.id
            }
        });
        ctx.body = yield render("crew.html",{
            employee,
            records
        });
    });
};